﻿<Assembly: WebResource("YCDotNetFramework.YCDotNetFramework.js", "application/x-javascript")> 


    <Themeable(True), ToolboxData("<{0}:YCTab runat=""server""></{0}:YCTab>")> _
    Public Class YCTab
        Inherits WebControl
        Implements INamingContainer

        Private pHfAbaAtiva As HtmlControls.HtmlInputHidden
        Private pHfUniqueID As String = String.Empty

        Private pYCAba As New List(Of YCAba)

        Private pContentControlContainer As Control = Nothing

        Private pModo As EnumModo

#Region "Propriedades"

        Public Enum EnumModo
            [Default] = 1
            Sequencial = 2
        End Enum

        <PersistenceMode(PersistenceMode.InnerProperty)> _
        Public ReadOnly Property YCAba() As List(Of YCAba)
            Get
                Return pYCAba
            End Get
        End Property
         
        Public Property CurrentTab As String
            Get
                Return pHfAbaAtiva.Value
            End Get
            Set(ByVal value As String)
                pHfAbaAtiva.Value = value
            End Set
        End Property

        <DefaultSettingValue("Default")> _
        Public Property Modo As EnumModo
            Get
                Return pModo
            End Get
            Set(ByVal value As EnumModo)
                pModo = value
            End Set
        End Property

        Public Property IndexSequencial As Integer
            Get
                Return Me.Attributes("IndexSequencial")
            End Get
            Set(ByVal value As Integer)
                Me.Attributes.Add("IndexSequencial", value)
            End Set
        End Property

#End Region

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)
            Page.ClientScript.RegisterClientScriptResource(GetType(YCTextBox), "YCDotNetFramework.YCDotNetFramework.js")
        End Sub

        Protected Overrides Sub CreateChildControls()
            Me.Controls.Clear()
            MyBase.CreateChildControls()

            Dim hdnField As New HtmlControls.HtmlInputHidden
            hdnField.ID = "pHfAbaAtiva"
            AddHandler hdnField.ServerChange, AddressOf HiddenFieldValueChanged
            MyBase.Controls.Add(hdnField)

            pHfAbaAtiva = hdnField


            pContentControlContainer = New Control

            For Each itemAba As YCAba In pYCAba
                If itemAba.Visible Then
                    If CurrentTab = "" Then CurrentTab = String.Concat(Me.ClientID, "_", itemAba.ClientID)

                    itemAba.Attributes.Add("class", String.Concat("YCTab_spanBody_Aba ", Me.ClientID, "_Aba"))

                    itemAba.Attributes.Add("CurrentTab", CurrentTab)
                    If CurrentTab <> itemAba.ClientID Then
                        itemAba.Style.Add("display", "none")
                    Else
                        itemAba.Style.Add("display", "")
                    End If

                    pContentControlContainer.Controls.Add(itemAba)

                End If
            Next

            Me.Controls.Add(pContentControlContainer)
        End Sub

        Private Sub HiddenFieldValueChanged(ByVal sender As Object, ByVal e As EventArgs)
            pHfAbaAtiva.Value = DirectCast(sender, HtmlControls.HtmlInputHidden).Value
        End Sub

        Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            MyBase.OnInit(e)
            MyBase.EnsureChildControls()
        End Sub

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            MyBase.RenderBeginTag(writer)
            pHfAbaAtiva.RenderControl(writer)

            With writer

                .Write("<span class=""YCTab_spanHead"">")

                For Each itemAba As YCAba In pYCAba
                    If itemAba.Visible Then
                        .Write(String.Concat("<a href=""javascript:void(0);"" class=""", IIf(itemAba.ClientID = CurrentTab, "YCTab_spanHead_a_Ativo", String.Empty), """ onclick=""YCMudarAba(this, '", Me.ClientID, "', '", itemAba.ClientID, "', '", pHfAbaAtiva.ClientID, "');"" >"))
                        .Write(itemAba.Titulo)
                        .Write("</a>")
                    End If
                Next

                .Write("</span>")

                .Write("<span class=""YCTab_spanBody"" >")

                For Each itemAba As YCAba In pYCAba
                    If CurrentTab <> itemAba.ClientID Then
                        itemAba.Style.Add("display", "none")
                    Else
                        itemAba.Style.Add("display", "")
                    End If
                Next

                pContentControlContainer.RenderControl(writer)
                .Write("</span>")
            End With

            MyBase.RenderEndTag(writer)
        End Sub
    End Class


